/*
 * This action is responsible for listing all the posts in the thread
 */

package airport.forum.action;

import java.util.Date;
import java.util.LinkedList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import airport.forum.pojo.Post;
import airport.forum.pojo.Thread;
import airport.forum.dao.ThreadDAO;

import com.opensymphony.xwork2.ActionSupport;
//post thread
public class PostThreadAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	//test push
	private static final long serialVersionUID = 1L;
	
	private long threadId;
	private Thread thread;
	private String threadTitle;
	private String threadContent;
	private String creator;
	private Date date;
	private long count;
	private String username = null;
	
	private LinkedList<Post> posts;
	
	protected HttpServletResponse servletResponse;  
	@Override   
	public void setServletResponse(HttpServletResponse servletResponse) {   
		this.servletResponse = servletResponse;   
	}   
	
	protected HttpServletRequest servletRequest;   
	@Override   
	public void setServletRequest(HttpServletRequest servletRequest) {   
		this.servletRequest = servletRequest;   
	}
	
	public String execute() {
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		
		System.out.println("Id:"+threadId);
		ThreadDAO dao = new ThreadDAO();
		thread = dao.getThread((long)threadId);
		threadTitle = thread.getThreadTitle();
		threadContent = thread.getOpeningPost();
		creator = thread.getUserName();
		date = thread.getDateCreated();
		count = thread.getPostCount();
		//post thread
		posts = new LinkedList<Post>();
		posts = dao.getPostsByPage((long)threadId, 1, 20);
		
		return "success";
	}

	public long getThreadId() {
		
		return threadId;
	}

	public void setThreadId(long threadId) {
		this.threadId = threadId;
	}

	public LinkedList<Post> getPosts() {
		return posts;
	}

	public void setPosts(LinkedList<Post> posts) {
		this.posts = posts;
	}

	public Thread getThread() {
		return thread;
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getThreadTitle() {
		return threadTitle;
	}

	public void setThreadTitle(String threadTitle) {
		this.threadTitle = threadTitle;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getThreadContent() {
		return threadContent;
	}

	public void setThreadContent(String threadContent) {
		this.threadContent = threadContent;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	
	
	

}
